package com.yobol.jgrowth.controller;

import com.yobol.jgrowth.commons.LoginStatus;
import com.yobol.jgrowth.domain.User;
import com.yobol.jgrowth.exception.PasswordNotTrueException;
import com.yobol.jgrowth.exception.UsernameDuplicationException;
import com.yobol.jgrowth.exception.UsernameNotFoundException;
import com.yobol.jgrowth.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * User Controller
 *
 * @author yobol
 * @create 2018-03-23 19:26
 **/

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/do_register",method = {RequestMethod.POST,RequestMethod.GET})
    public String do_register(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "username") String name,@RequestParam(value = "password") String pwd)  {

        String redirectUrl = "/register";

        if (name != null && pwd != null){
            User user = new User();
            user.setName(name);
            user.setPwd(pwd);
            user.setRegisterTime(new Date(System.currentTimeMillis()));
            user.setRid(new Integer(LoginStatus.USER));

            try{
                if(userService.addUser(user) > 0){
                    redirectUrl = "/login";
                }
            } catch (UsernameDuplicationException e){
                e.printStackTrace();
                request.getSession().setAttribute("error",e.toString());
                redirectUrl = "/register";
            }
        }
        return "redirect:" + redirectUrl;
    }

    @RequestMapping(value = "/do_login",method = {RequestMethod.POST,RequestMethod.GET})
    public String do_login(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "username") String name,@RequestParam(value = "password") String pwd){
        //System.out.println(request.getServletContext().getRealPath("/static/ckfinder.xml"));
        String redirectUrl = "/login";
        if(name != null && pwd != null){
            User user = new User();
            user.setName(name);
            user.setPwd(pwd);
            //设置目录
            //new CKFinder().chooseUserFolder("D:\\Projects\\JGrowth\\src\\main\\resources\\static\\ckfinder.xml","images",name);

            try{
                user = userService.validateUser(user);
                request.getSession().setAttribute("uid",user.getUid());
                request.getSession().setAttribute("username",name);
                request.getSession().setAttribute("avatar","img/dog.jpg");
                request.getSession().setAttribute("latest_online_time",user.getLatestOnlineTime());
                request.getSession().setAttribute("login_status",user.getRid());
                if (user.getRid() > 1){
                    request.getSession().setAttribute("users",userService.selectAll());
                }
                redirectUrl = "/index";
            } catch (UsernameNotFoundException e) {
                e.printStackTrace();
                request.getSession().setAttribute("error",e.toString());
                redirectUrl = "/login";
            } catch (PasswordNotTrueException e) {
                e.printStackTrace();
                request.getSession().setAttribute("error",e.toString());
                redirectUrl = "/login";
            }
        }
        return "redirect:" + redirectUrl;
    }

    @RequestMapping(value = "/do_loginOut")
    public void loginOut(HttpServletRequest request,HttpServletResponse response) throws IOException {
        request.getSession().invalidate();
        response.sendRedirect("/index");
    }
}
